package example.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class User implements Serializable {
    public String name;
    public String password;
    private List<Post> posts = new LinkedList<Post>();
    private List<Post> iterablePosts;

    public synchronized List<Post> getPosts() {
        if (iterablePosts == null) {
            iterablePosts = new ArrayList<Post>(posts);
        }
        return iterablePosts;
    }

    public synchronized void addPost(User user, Date date, String message) {
        Post post = new Post();
        post.user = user;
        post.date = date;
        post.message = message;

        posts.add(0, post);
        iterablePosts = null;
    }
}